package routers

import (
	"encoding/json"
	"fmt"
	"net/http"

	"shqsoft.cn/pasnj/base"
	"shqsoft.cn/pasnj/xmla"

	"github.com/labstack/echo"
)

//卡片的mdx
var mdx_String = `WITH 
MEMBER [Measures].[ZCKRJ2]
AS ([科目].[基本类别].&[1],[Measures].[对公日均])+[Measures].[储蓄日均]+[Measures].[个人保本理财日均]
MEMBER [Measures].[ZCKSD]
AS ([科目].[基本类别].&[1],[Measures].[对公余额])+[Measures].[储蓄余额]+[Measures].[个人保本理财余额]
MEMBER [Measures].[ZCKSD_BSR]
AS [Measures].[ZCKSD]-([日期].[时间].currentmember.prevmember,[Measures].[ZCKSD])
MEMBER [Measures].[ZCKSD_BSN]
AS [Measures].[ZCKSD]-(ANCESTOR([日期].[时间].currentmember,[日期].[时间].[年]).prevmember,[Measures].[ZCKSD])

MEMBER [Measures].[ZCKRJ]
AS SUM(PERIODSTODATE([日期].[时间].[年],[日期].[时间].currentmember),[Measures].[ZCKSD])/
   COUNT(PERIODSTODATE([日期].[时间].[年],[日期].[时间].currentmember))
MEMBER [Measures].[ZCKRJ_BSR]
AS [Measures].[ZCKRJ]-SUM(PERIODSTODATE([日期].[时间].[年],[日期].[时间].currentmember.prevmember),[Measures].[ZCKSD])/
   COUNT(PERIODSTODATE([日期].[时间].[年],[日期].[时间].currentmember.prevmember))
MEMBER [Measures].[ZCKRJ_BSN]
AS [Measures].[ZCKRJ]-(ANCESTOR([日期].[时间].currentmember,[日期].[时间].[年]).prevmember,[Measures].[ZCKRJ2])

MEMBER [Measures].[ZDKSD]
AS ([科目].[基本类别].&[2],[Measures].[对公余额])+([个贷帐户].[是否证券化].&[False],[Measures].[个贷余额])
MEMBER [Measures].[ZDKSD_BSR]
AS [Measures].[ZDKSD]-([日期].[时间].currentmember.prevmember,[Measures].[ZDKSD])
MEMBER [Measures].[ZDKSD_BSN]
AS [Measures].[ZDKSD]-(ANCESTOR([日期].[时间].currentmember,[日期].[时间].[年]).prevmember,[Measures].[ZDKSD])

MEMBER [Measures].[GCBFZSD]
AS ([科目].[二级].&[14],[Measures].[对公余额])+([科目].[二级].&[19],[Measures].[对公余额])+([科目].[二级].&[56],[Measures].[储蓄余额])+[Measures].[个人保本理财余额]
MEMBER [Measures].[GCBFZSD_BSR]
AS [Measures].[GCBFZSD]-([日期].[时间].currentmember.prevmember,[Measures].[GCBFZSD])
MEMBER [Measures].[GCBFZSD_BSN]
AS [Measures].[GCBFZSD]-(ANCESTOR([日期].[时间].currentmember,[日期].[时间].[年]).prevmember,[Measures].[GCBFZSD])


SELECT {[Measures].[ZCKSD],[Measures].[ZCKSD_BSR],[Measures].[ZCKSD_BSN],
        [Measures].[ZCKRJ],[Measures].[ZCKRJ_BSR],[Measures].[ZCKRJ_BSN],
		[Measures].[ZDKSD],[Measures].[ZDKSD_BSR],[Measures].[ZDKSD_BSN],
		[Measures].[GCBFZSD],[Measures].[GCBFZSD_BSR],[Measures].[GCBFZSD_BSN]
		} on 0
from [Yybankdw]
where ([日期].[日期].&[%s],%s)`

//柱状图的mdx
var mdx_String1 = `WITH 
MEMBER [Measures].[GSCKRJ]
AS ([科目].[基本类别].&[1],[Measures].[对公日均])/10000
MEMBER [Measures].[CXCKRJ]
AS ([Measures].[储蓄日均]+[Measures].[个人保本理财日均])/10000
MEMBER [Measures].[GSKHS]
AS [Measures].[DG有效户]
MEMBER [Measures].[LSKHS]
AS [Measures].[客户数量]
select {[Measures].[GSCKRJ],[Measures].[CXCKRJ],[Measures].[GSKHS],[Measures].[LSKHS]} on 0,
TAIL([日期].[月].[月].members,12) on 1
FROM [Yybankdw]
WHERE (%s)`

//HomeResponse3 首页相应
type HomeResponse3 struct {
	Title2 string             `json:"title2"`
	List2  []HomeResponseCard `json:"list2"`
}

//HomeResponseCard 首页卡片
type HomeResponseCard struct {
	ZCKSD     *float64 `json:"ZCKSD"`
	ZCKSD_BSR *float64 `json:"ZCKSD_BSR"`
	ZCKSD_BSN *float64 `json:"ZCKSD_BSN"`

	ZCKRJ     *float64 `json:"ZCKRJ"`
	ZCKRJ_BSR *float64 `json:"ZCKRJ_BSR"`
	ZCKRJ_BSN *float64 `json:"ZCKRJ_BSN"`

	ZDKSD     *float64 `json:"ZDKSD"`
	ZDKSD_BSR *float64 `json:"ZDKSD_BSR"`
	ZDKSD_BSN *float64 `json:"ZDKSD_BSN"`

	GCBFZSD     *float64 `json:"GCBFZSD"`
	GCBFZSD_BSR *float64 `json:"GCBFZSD_BSR"`
	GCBFZSD_BSN *float64 `json:"GCBFZSD_BSN"`
}

//ChartData 每个图表的数据
type ChartData struct {
	Month1      *float64 `json:"Month1"`
	Month2      *float64 `json:"Month2"`
	Month3      *float64 `json:"Month3"`
	Month4      *float64 `json:"Month4"`
	Month5      *float64 `json:"Month5"`
	Month6      *float64 `json:"Month6"`
	Month7      *float64 `json:"Month7"`
	Month8      *float64 `json:"Month8"`
	Month9      *float64 `json:"Month9"`
	Month10     *float64 `json:"Month10"`
	Month11     *float64 `json:"Month11"`
	Month12     *float64 `json:"Month12"`
	MonthName1  string   `json:"MonthName1"`
	MonthName2  string   `json:"MonthName2"`
	MonthName3  string   `json:"MonthName3"`
	MonthName4  string   `json:"MonthName4"`
	MonthName5  string   `json:"MonthName5"`
	MonthName6  string   `json:"MonthName6"`
	MonthName7  string   `json:"MonthName7"`
	MonthName8  string   `json:"MonthName8"`
	MonthName9  string   `json:"MonthName9"`
	MonthName10 string   `json:"MonthName10"`
	MonthName11 string   `json:"MonthName11"`
	MonthName12 string   `json:"MonthName12"`
}

//HomeRequestCardData 首页请求卡片数据
func HomeRequestCardData(c echo.Context) (err error) {
	var req HomeRequest
	jsonStr := c.FormValue("HomeRequest")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "HomeRequest不能为空！")
	}
	err = json.Unmarshal([]byte(jsonStr), &req)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	data, err := execRequestMdx(&req)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	return c.JSON(http.StatusOK, data)

}

//HomeRequestChartData 首页请求图表数据
func HomeRequestChartData(c echo.Context) (err error) {
	var req HomeRequest
	jsonStr := c.FormValue("HomeRequest")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "HomeRequest不能为空！")
	}
	err = json.Unmarshal([]byte(jsonStr), &req)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	data, err := execRequestMdx1(&req)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	return c.JSON(http.StatusOK, data)
}

func execRequestMdx(req *HomeRequest) (data []HomeResponseCard, err error) {
	mdx := fmt.Sprintf(mdx_String, req.EndDate, req.UniqueName)
	port := new(xmla.DefaultXMLAPort)
	port.SetUrl(base.Context.Xmla.Url)
	p := &xmla.XMLAExecuteProperties{
		DataSourceInfo: base.Context.Xmla.DataSourceInfo,
		Catalog:        base.Context.Xmla.Catalog,
		Format:         "Multidimensional",
		Content:        "Data",
		AxisFormat:     "TupleFormat",
	}
	//data1 := CardAndChartData{}
	cData, err := port.Execute(&mdx, p)
	if err != nil {
		return nil, err
	}
	if cData.RowAxis == nil {
		return
	}
	rowAxis := *cData.RowAxis
	rowLen := len(rowAxis)
	if rowLen <= 0 {
		return nil, err
	}
	var resps1 []HomeResponseCard
	resps1 = make([]HomeResponseCard, rowLen, rowLen)
	for i, _ := range rowAxis {
		j := i
		resps1[j].ZCKSD = base.ToFloat64(cData.CellData[i][0])
		resps1[j].ZCKSD_BSR = base.ToFloat64(cData.CellData[i][1])
		resps1[j].ZCKSD_BSN = base.ToFloat64(cData.CellData[i][2])
		resps1[j].ZCKRJ = base.ToFloat64(cData.CellData[i][3])
		resps1[j].ZCKRJ_BSR = base.ToFloat64(cData.CellData[i][4])
		resps1[j].ZCKRJ_BSN = base.ToFloat64(cData.CellData[i][5])
		resps1[j].ZDKSD = base.ToFloat64(cData.CellData[i][6])
		resps1[j].ZDKSD_BSR = base.ToFloat64(cData.CellData[i][7])
		resps1[j].ZDKSD_BSN = base.ToFloat64(cData.CellData[i][8])
		resps1[j].GCBFZSD = base.ToFloat64(cData.CellData[i][9])
		resps1[j].GCBFZSD_BSR = base.ToFloat64(cData.CellData[i][10])
		resps1[j].GCBFZSD_BSN = base.ToFloat64(cData.CellData[i][11])
	}
	data = resps1
	return

}

//获取图标数据
func execRequestMdx1(req *HomeRequest) (data []ChartData, err error) {
	//mdx := fmt.Sprintf(mdx_String1, "[员工].[所属部门].[All]")
	mdx := fmt.Sprintf(mdx_String1, req.UniqueName)
	port := new(xmla.DefaultXMLAPort)
	port.SetUrl(base.Context.Xmla.Url)
	p := &xmla.XMLAExecuteProperties{
		DataSourceInfo: base.Context.Xmla.DataSourceInfo,
		Catalog:        base.Context.Xmla.Catalog,
		Format:         "Multidimensional",
		Content:        "Data",
		AxisFormat:     "TupleFormat",
	}
	cData, err := port.Execute(&mdx, p)
	if err != nil {
		return nil, err
	}
	if cData.RowAxis == nil {
		return
	}
	rowAxis := *cData.RowAxis
	rowLen := len(rowAxis)
	if rowLen <= 0 {
		return nil, err
	}
	var cd []ChartData
	cd = make([]ChartData, rowLen, rowLen)

	for i := 0; i < 12; i++ {
		if i == 0 {
			for j := 0; j < 4; j++ {
				cd[j].Month1 = base.ToFloat64(cData.CellData[i][j])
				cd[j].MonthName1 = rowAxis[i][0].Caption
			}
		}
		if i == 1 {
			for j := 0; j < 4; j++ {
				cd[j].Month2 = base.ToFloat64(cData.CellData[i][j])
				cd[j].MonthName2 = rowAxis[i][0].Caption
			}
		}
		if i == 2 {
			for j := 0; j < 4; j++ {
				cd[j].Month3 = base.ToFloat64(cData.CellData[i][j])
				cd[j].MonthName3 = rowAxis[i][0].Caption
			}
		}
		if i == 3 {
			for j := 0; j < 4; j++ {
				cd[j].Month4 = base.ToFloat64(cData.CellData[i][j])
				cd[j].MonthName4 = rowAxis[i][0].Caption
			}
		}
		if i == 4 {
			for j := 0; j < 4; j++ {
				cd[j].Month5 = base.ToFloat64(cData.CellData[i][j])
				cd[j].MonthName5 = rowAxis[i][0].Caption
			}

		}
		if i == 5 {
			for j := 0; j < 4; j++ {
				cd[j].Month6 = base.ToFloat64(cData.CellData[i][j])
				cd[j].MonthName6 = rowAxis[i][0].Caption
			}
		}
		if i == 6 {
			for j := 0; j < 4; j++ {
				cd[j].Month7 = base.ToFloat64(cData.CellData[i][j])
				cd[j].MonthName7 = rowAxis[i][0].Caption
			}
		}
		if i == 7 {
			for j := 0; j < 4; j++ {
				cd[j].Month8 = base.ToFloat64(cData.CellData[i][j])
				cd[j].MonthName8 = rowAxis[i][0].Caption
			}
		}
		if i == 8 {
			for j := 0; j < 4; j++ {
				cd[j].Month9 = base.ToFloat64(cData.CellData[i][j])
				cd[j].MonthName9 = rowAxis[i][0].Caption
			}
		}
		if i == 9 {
			for j := 0; j < 4; j++ {
				cd[j].Month10 = base.ToFloat64(cData.CellData[i][j])
				cd[j].MonthName10 = rowAxis[i][0].Caption
			}
		}
		if i == 10 {
			for j := 0; j < 4; j++ {
				cd[j].Month11 = base.ToFloat64(cData.CellData[i][j])
				cd[j].MonthName11 = rowAxis[i][0].Caption
			}
		}
		if i == 11 {
			for j := 0; j < 4; j++ {
				cd[j].Month12 = base.ToFloat64(cData.CellData[i][j])
				cd[j].MonthName12 = rowAxis[i][0].Caption
			}
		}

	}

	// for i, rowTuples := range rowAxis {

	// 	//k := len(rowTuples) - 1
	// 	resps1[i].chartName = rowTuples[i].Caption
	// 	resps1[i].cd[i].Month1 = base.ToFloat64(cData.CellData[i][0])
	// 	resps1[i].cd[i].Month2 = base.ToFloat64(cData.CellData[i][1])
	// 	resps1[i].cd[i].Month3 = base.ToFloat64(cData.CellData[i][2])
	// 	resps1[i].cd[i].Month4 = base.ToFloat64(cData.CellData[i][3])
	// 	resps1[i].cd[i].Month5 = base.ToFloat64(cData.CellData[i][4])
	// 	resps1[i].cd[i].Month6 = base.ToFloat64(cData.CellData[i][5])
	// 	resps1[i].cd[i].Month7 = base.ToFloat64(cData.CellData[i][6])
	// 	resps1[i].cd[i].Month8 = base.ToFloat64(cData.CellData[i][7])
	// 	resps1[i].cd[i].Month9 = base.ToFloat64(cData.CellData[i][8])
	// 	resps1[i].cd[i].Month10 = base.ToFloat64(cData.CellData[i][9])
	// 	resps1[i].cd[i].Month11 = base.ToFloat64(cData.CellData[i][10])
	// 	resps1[i].cd[i].Month12 = base.ToFloat64(cData.CellData[i][11])
	// }
	data = cd
	return
}
